package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.activity.StoreActivity;
import top.drinyeit.nongmao.app.Constant;
import top.drinyeit.nongmao.model.CategoryGoodsDetailsBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;
import top.drinyeit.nongmao.util.SPUtil;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/9.
 */

public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder> implements View.OnClickListener {
    private Context context;
    private List<CategoryGoodsDetailsBean> list;
    private Handler mHandler;

    public CategoryAdapter(List<CategoryGoodsDetailsBean> list, Handler mHandler) {
        this.list = list;
        this.mHandler = mHandler;
    }

    public void clear() {
        list.clear();
    }

    public List<CategoryGoodsDetailsBean> getData() {
        return list;
    }

    public void addData(List<CategoryGoodsDetailsBean> list) {
        this.list.addAll(list);
    }

    public void setData(List<CategoryGoodsDetailsBean> list) {
        this.list = list;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @Override
    public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        View v = LayoutInflater.from(context).inflate(R.layout.item_category_detail, parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        v.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(CategoryAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        Glide.with(context).load(list.get(position).getCover_img_url())
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontTransform()
                .placeholder(R.drawable.ic_def)
                .into(holder.shop_im);
        holder.stockNumber.setText("库存剩余" + list.get(position).getStock_number() + "份");
        holder.commodity_name.setText(list.get(position).getCommodity_name());
        holder.weight.setText("/" + list.get(position).getWeight() + "g");
        holder.price.setText("¥" + list.get(position).getSale_price());
        holder.shop_name.setText(list.get(position).getStore_name());
        if (holder.shop_name.getText().toString().equals("")) {
            holder.shop.setVisibility(View.GONE);
        }
        if (list.get(position).getStock_number() == 0) {
            holder.buy.setBackgroundColor(ContextCompat.getColor(context, R.color.gray_189));
        }

    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView shop_im;
        public TextView commodity_name, weight;
        public TextView shop_name;
        public TextView stockNumber;
        public TextView price;
        public ImageView buy;
        public LinearLayout shop;
        public RelativeLayout item;
//        public View itemView;

        public ViewHolder(View itemView) {
            super(itemView);
            shop_im = itemView.findViewById(R.id.shop_im);
            commodity_name = itemView.findViewById(R.id.commodity_name);
            shop_name = itemView.findViewById(R.id.shop_name);
            price = itemView.findViewById(R.id.price);
            buy = itemView.findViewById(R.id.buy);
            weight = itemView.findViewById(R.id.tv_weight);
            item = itemView.findViewById(R.id.rl_item);
            shop = itemView.findViewById(R.id.ll_shop);
            stockNumber = itemView.findViewById(R.id.tv_stock_number);
//            this.itemView  = itemView;

//            item.setOnClickListener(this);
            buy.setOnClickListener(this);
            shop.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Intent intent;
            switch (v.getId()) {
                case R.id.buy:
                    buy();
                    break;

                case R.id.ll_shop:
                    intent = new Intent(context, StoreActivity.class);
                    intent.putExtra("store_id", list.get(getAdapterPosition()).getStore_id());
                    intent.putExtra("store_name", list.get(getAdapterPosition()).getStore_name());
                    intent.putExtra("", list.get(getAdapterPosition()).getCover_img_url());
                    context.startActivity(intent);
                    break;
//                case R.id.rl_item:
//                    intent = new Intent(context, CommodityDetailActivity.class);
//                    intent.putExtra("id", list.get(getAdapterPosition()).getId());
//                    context.startActivity(intent);
//                    break;
            }

        }

        //添加到购物车
        private void buy() {
            if (list.get(getAdapterPosition()).getStock_number() < 1) {
                return;
            }
            if (SPUtil.getShareString(Constant.USER_ID).equals("")) {
                Toast.makeText(context, "请先登录", Toast.LENGTH_SHORT).show();
                return;
            }
            Map<String, Object> map = new HashMap<>();
            String user_id = SPUtil.getShareString(Constant.USER_ID);
            map.put("action", "commodity_add_shopping_cart");
            map.put("user_id", user_id);
            map.put("commodity_id", list.get(getAdapterPosition()).getId());
            map.put("commodity_sum", 1);
            CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
                @Override
                public void onError(String e, int code) {
                    Toast.makeText(context, "添加购物车失败", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onNext(HttpResponse<String> o) {
                    if (o.getCode() == COMMON_SUCCESS) {
                        if (mHandler != null) {
                            Message message = Message.obtain(mHandler, 0x123);
                            message.sendToTarget();
                        }
                    }
                    Toast.makeText(context, o.getMessage(), Toast.LENGTH_SHORT).show();
                }
            });
            RetrofitMethod.getInstance().addCart(subscriber, map);
        }

    }
}

